package com.yyfax.ph.home.recommendloan;

import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Model;
import com.yyfax.infrastructure.network.NetworkHelper;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yyfax.infrastructure.network.entity.EmptyEntity;
import com.yyfax.ph.base.YJPH;
import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.POST;
import rx.Observable;

import java.io.Serializable;

/**
 * 类描述： 获取推荐贷款的权限
 */
public class RecommendPermissionModel extends Model<BaseResultEntity> {
	private static final String TAG = RecommendPermissionModel.class.getSimpleName();

	@Override
	public void onNext(BaseResultEntity entity) {
		switch (entity.requestCode) {
			case RecommendPermissionPresenter.FUNCTION_CODE_RECOMMEND_LAS:
				LASShareResult lasShare = cast(entity);
				EventAgent.post(lasShare);
				break;
			case RecommendPermissionPresenter.FUNCTION_CODE_RECOMMEND_LOAN:
				PermissionResult result = cast(entity);
				EventAgent.post(result);
				break;
			default:
				break;
		}
	}

	@Override
	public Observable getObservable(Object params, int functionalCode) {
		Service service = (Service) NetworkHelper.createService(Service.class);
		String token = YJPH.getInstance().getPreferenceManager().getLoginToken();
		switch (functionalCode) {
			case RecommendPermissionPresenter.FUNCTION_CODE_RECOMMEND_LAS:
				return service.getLASShareInfo(token, new EmptyEntity());
			case RecommendPermissionPresenter.FUNCTION_CODE_RECOMMEND_LOAN:
				return service.getRcmAllowed(token, new EmptyEntity());
			default:
				return null;
		}

	}

	private interface Service {
		@POST("apply/permission/getRcmdAllowed")
		Observable<PermissionResult> getRcmAllowed(@Header("token") String token, @Body EmptyEntity notice);

		@POST("apply/rcmd/shareH5")
		Observable<LASShareResult> getLASShareInfo(@Header("token") String token, @Body EmptyEntity emptyEntity);
	}

	public static class PermissionBen {
		public String code;
		public String companyName;
		public String companyPhone;
	}

	public static class PermissionResult extends BaseResultEntity<PermissionBen> {

	}

	public static class LASShareResult extends BaseResultEntity<ShareBean> {
	}

	public static class ShareBean implements Serializable {
		private static final long serialVersionUID = 4747484788064728917L;

		public String companyName;
		public String companyPhone;
		public String content;
		public String title;
		public String url;
		public String code;
		public String left = "";

		public ShareBean() {
		}

		public ShareBean(String content, String title, String url) {
			this.content = content;
			this.title = title;
			this.url = url;
		}

		public ShareBean(String content, String title, String url, String code) {
			this.content = content;
			this.title = title;
			this.url = url;
			this.code = code;
		}

		@Override
		public String toString() {
			return "ShareBean [content=" + content + ", title=" + title + ", url=" + url + "]";
		}

		public String getContent() {
			return content;
		}

		public void setContent(String content) {
			this.content = content;
		}

		public String getTitle() {
			return title;
		}

		public void setTitle(String title) {
			this.title = title;
		}

		public String getUrl() {
			return url;
		}

		public void setUrl(String url) {
			this.url = url;
		}
	}
}
